/*
 * @Author: 姚沈阳 11500793+yao-shenyang@user.noreply.gitee.com
 * @Date: 2023-10-31 11:37:32
 * @LastEditors: 姚沈阳 11500793+yao-shenyang@user.noreply.gitee.com
 * @LastEditTime: 2023-11-04 08:57:02
 * @FilePath: \票务管理\app\ticketing-management\server\app\service\ticket.js
 * @Description: 
 * qq1908552310
 */
const Service = require('egg').Service;

class TicketService extends Service {
    async index() {
        return await this.app.mysql.select('trains');
    }
    async create({ TrainType, TrainNumber, TrainManager, Seat, Carriage, RideLimitation, WithoutSeat, getTime }) {
        const [date, time] = getTime.split(' '); // 将日期和时间拆分成两个部分
        const [year, month, day] = date.split('-'); // 将日期拆分成年、月、日三个部分
        const paddedDay = day.padStart(2, '0'); // 使用 padStart() 方法添加前导零
        const formattedTime = `${year}-${month}-${paddedDay} ${time}`; // 组合成新的格式化时间

        let $params = {
            train_number: TrainNumber,
            train_type: TrainType,
            train_manager: TrainManager,
            total_seats: Seat,
            carriage_number: Carriage,
            passenger_limit: RideLimitation,
            created_at: formattedTime,
            status: 1
        }
        return await this.app.mysql.insert('trains', $params)
    }

    async update({ id, TrainType, TrainNumber, TrainManager, Seat, Carriage, RideLimitation, WithoutSeat }) {
        let $params = {
            id: id,
            train_number: TrainNumber,
            train_type: TrainType,
            train_manager: TrainManager,
            total_seats: Seat,
            carriage_number: Carriage,
            passenger_limit: RideLimitation,
        }
        return await this.app.mysql.update('trains', $params)
    }

    async destroy({ id }) {
        return await this.app.mysql.delete('trains', { id: id })
    }
}

module.exports = TicketService;